#include "CustomSceneXml.h"

CustomSceneXml::CustomSceneXml()
{
}


bool CustomSceneXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "CustomScene")   //解析自动化
        {
            CustomSceneStruct customScene;

            QDomNode customSceneChild = rootChild.toElement().firstChild();
            while(!customSceneChild.isNull())
            {
                if(customSceneChild.toElement().tagName() == "Name")
                {
                    QString name = customSceneChild.toElement().text();
                    customScene.setName(name);
                }
                if(customSceneChild.toElement().tagName() == "Id")
                {
                    quint64 id = customSceneChild.toElement().text().toUInt();
                    customScene.setId(id);
                }
                if(customSceneChild.toElement().tagName() == "IcoId")
                {
                    quint16 icoId = customSceneChild.toElement().text().toUInt();
                    customScene.setIcoId(icoId);
                }

                if(customSceneChild.toElement().tagName() == "ActionList")
                {
                    QDomNode actionListChild = customSceneChild.toElement().firstChild();
                    QList<ActionStruct> actionList;
                    while(!actionListChild.isNull())
                    {
                        if (actionListChild.toElement().tagName() == "Action")
                        {
                            ActionStruct action;
                            QDomNode actionChild = actionListChild.toElement().firstChild();
                            while(!actionChild.isNull())
                            {
                                if(actionChild.toElement().tagName() == "ZoneName")
                                {
                                    QString zoneName = actionChild.toElement().text();
                                    action.setZoneName(zoneName);
                                }
                                if(actionChild.toElement().tagName() == "DeviceName")
                                {
                                    QString deviceName = actionChild.toElement().text();
                                    action.setDeviceName(deviceName);
                                }
                                if(actionChild.toElement().tagName() == "FunctionName")
                                {
                                    QString functionName = actionChild.toElement().text();
                                    action.setFunctionName(functionName);
                                }
                                if(actionChild.toElement().tagName() == "ValueName")
                                {
                                    QString valueName = actionChild.toElement().text();
                                    action.setValueName(valueName);
                                }
                                if(actionChild.toElement().tagName() == "ZoneCode")
                                {
                                    quint16 zoneCode = actionChild.toElement().text().toUInt();
                                    action.setZoneCode(zoneCode);
                                }
                                if(actionChild.toElement().tagName() == "DeviceTypeCode")
                                {
                                    quint16 deviceTypeCode = actionChild.toElement().text().toUInt();
                                    action.setDeviceTypeCode(deviceTypeCode);
                                }
                                if(actionChild.toElement().tagName() == "DeviceCode")
                                {
                                    quint16 deviceCode = actionChild.toElement().text().toUInt();
                                    action.setDeviceCode(deviceCode);
                                }
                                if(actionChild.toElement().tagName() == "FunctionCode")
                                {
                                    quint16 functionCode = actionChild.toElement().text().toUInt();
                                    action.setFunctionCode(functionCode);
                                }
                                if(actionChild.toElement().tagName() == "FunctionValue")
                                {
                                    quint16 functionValue = actionChild.toElement().text().toUInt();
                                    action.setFunctionValue(functionValue);
                                }
                                if(actionChild.toElement().tagName() == "Address")
                                {
                                    quint16 address = actionChild.toElement().text().toUInt();
                                    action.setAddress(address);
                                }
                                if(actionChild.toElement().tagName() == "DelayTime")
                                {
                                    quint16 delayTime = actionChild.toElement().text().toUInt();
                                    action.setDelayTime(delayTime);
                                }
                                actionChild = actionChild.nextSibling();
                            }
                            actionList.append(action);
                        }
                        actionListChild = actionListChild.nextSibling();
                    }
                    customScene.setActionList(actionList);
                }

                customSceneChild = customSceneChild.nextSibling();
            }
            m_customSceneList.append(customScene);
        }
        rootChild = rootChild.nextSibling();
    }
    return true;
}

bool CustomSceneXml::modify(const CustomSceneStruct &customScene)
{
    if(!this->check())
    {
        return false;
    }

    m_customSceneList.append(customScene);

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();
    QDomText domText;

    QDomElement customSceneElement = doc.createElement("CustomScene");

    QDomElement nameElement = doc.createElement("Name");
    QString name = customScene.name();
    domText = doc.createTextNode(name);
    nameElement.appendChild(domText);
    customSceneElement.appendChild(nameElement);

    QDomElement idElement = doc.createElement("Id");
    QString id = QString::number(customScene.id(), 10);
    domText = doc.createTextNode(id);
    idElement.appendChild(domText);
    customSceneElement.appendChild(idElement);


    QDomElement icoIdElement = doc.createElement("IcoId");
    QString icoId = QString::number(customScene.icoId(), 10);
    domText = doc.createTextNode(icoId);
    icoIdElement.appendChild(domText);
    customSceneElement.appendChild(icoIdElement);

    QDomElement actionListElement = doc.createElement("ActionList");
    for(int i=0; i<customScene.actionList().size(); i++)
    {
        ActionStruct action = customScene.actionList().at(i);
        QDomElement actionElement = doc.createElement("Action");

        QDomElement zoneNameElement = doc.createElement("ZoneName");
        QString zoneName = action.zoneName();
        domText = doc.createTextNode(zoneName);
        zoneNameElement.appendChild(domText);
        actionElement.appendChild(zoneNameElement);

        QDomElement deviceNameElement = doc.createElement("DeviceName");
        QString deviceName =action.deviceName();
        domText = doc.createTextNode(deviceName);
        deviceNameElement.appendChild(domText);
        actionElement.appendChild(deviceNameElement);

        QDomElement functionNameElement = doc.createElement("FunctionName");
        QString functionName = action.functionName();
        domText = doc.createTextNode(functionName);
        functionNameElement.appendChild(domText);
        actionElement.appendChild(functionNameElement);

        QDomElement valueNameElement = doc.createElement("ValueName");
        QString valueName = action.valueName();
        domText = doc.createTextNode(valueName);
        valueNameElement.appendChild(domText);
        actionElement.appendChild(valueNameElement);

        QDomElement zoneCodeElement = doc.createElement("ZoneCode");
        QString zoneCode = QString::number(action.zoneCode(), 10);
        domText = doc.createTextNode(zoneCode);
        zoneCodeElement.appendChild(domText);
        actionElement.appendChild(zoneCodeElement);

        QDomElement deviceTypeCodeElement = doc.createElement("DeviceTypeCode");
        QString deviceTypeCode = QString::number(action.deviceTypeCode(), 10);
        domText = doc.createTextNode(deviceTypeCode);
        deviceTypeCodeElement.appendChild(domText);
        actionElement.appendChild(deviceTypeCodeElement);

        QDomElement deviceCodeElement = doc.createElement("DeviceCode");
        QString deviceCode = QString::number(action.deviceCode(), 10);
        domText = doc.createTextNode(deviceCode);
        deviceCodeElement.appendChild(domText);
        actionElement.appendChild(deviceCodeElement);

        QDomElement functionCodeElement = doc.createElement("FunctionCode");
        QString functionCode = QString::number(action.functionCode(), 10);
        domText = doc.createTextNode(functionCode);
        functionCodeElement.appendChild(domText);
        actionElement.appendChild(functionCodeElement);

        QDomElement functionValueElement = doc.createElement("FunctionValue");
        QString functionValue = QString::number(action.functionValue(), 10);
        domText = doc.createTextNode(functionValue);
        functionValueElement.appendChild(domText);
        actionElement.appendChild(functionValueElement);

        QDomElement addressElement = doc.createElement("Address");
        QString address = QString::number(action.address(), 10);
        domText = doc.createTextNode(address);
        addressElement.appendChild(domText);
        actionElement.appendChild(addressElement);

        QDomElement delayTimeElement = doc.createElement("DelayTime");
        QString delayTime = QString::number(action.delayTime(), 10);
        domText = doc.createTextNode(delayTime);
        delayTimeElement.appendChild(domText);
        actionElement.appendChild(delayTimeElement);

        actionListElement.appendChild(actionElement);
    }
    customSceneElement.appendChild(actionListElement);

    root.appendChild(customSceneElement);

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    return true;
}



bool CustomSceneXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_customSceneList.clear();
    return true;
}

QList<ActionStruct> CustomSceneXml::actionList(const quint64 &sceneId)
{
    QList<ActionStruct> actionList;
    for(int i=0; i<m_customSceneList.size(); i++)
    {
        CustomSceneStruct customScene = m_customSceneList.at(i);
        if(sceneId == customScene.id())
        {
            actionList = customScene.actionList();
        }
    }
    return actionList;
}



QList<CustomSceneStruct> CustomSceneXml::customSceneList() const
{
    return m_customSceneList;
}

